Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Why am I getting "cannot set headers after they are send"?

I am creating a ZIP compressor using the ADM-ZIP library and Multer for uploading the files. The code does work when I hit the download button and I do get the compressed file, but after the server breaks stating "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"

app.js

const express = require("express");
const multer = require("multer");
const admzip = require("adm-zip");

const fs = require("fs");
const path = require("path");

const app = express();

app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(cors());

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "public/uploads");
  },
  filename: (req, file, cb) => {
    cb(
      null,
      file.filename + "-" + Date.now() + path.extname(file.originalname)
    );
  },
});

const maxSize = 10 * 1024 * 1024;
const compressFileUpload = multer({
  storage,
  limits: {
    filesize: maxSize,
  },
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

app.post("/compress", compressFileUpload.array("file", 100), (req, res) => {
    const zip = new admzip();
    if (req.files) {
        req.files.map(file => {
            console.log(file.path);
            zip.addLocalFile(file.path);
        });

        const output = Date.now() + "output.zip";
        fs.writeFileSync(output, zip.toBuffer());

        res.download(output, (err) => {
            res.send("Error in downloading zip file.")
        })
    }
});

app.listen(process.env.PORT || 5000, () => {
  console.log("server running on PORT 5000...");
});
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!